<?php

namespace app\common\model\order;


use app\common\model\BaseModel;

/**
 * Class OrderTrade
 */
class OrderTrade extends BaseModel
{
    protected $name = 'order_trade';
    protected $pk = 'id';

    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $model = self::assWhere($model, $where);
        $list = $model->paginate($params);
        return $list;
    }

    public static function createData($where)
    {
        $model = new static();
        $where['app_id'] = self::$app_id;
        $data = $model->save($where);
        return $data;
    }

    public static function one($where)
    {
        $where = self::disposeWhere($where);
        $model = new static();
        return $model->findOrFail($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = self::assWhere($model, $where);
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $list = $model->select();
        return $list;
    }

    /**
     * 关联订单表
     */
    public function orderList()
    {
        return $this->hasMany('app\\common\\model\\order\\Order', 'order_id', 'order_id');
    }

    /**
     * 详情
     */
    public static function detail($where, $with = [])
    {
        is_array($where) ? $filter = $where : $filter['id'] = (int)$where;
        return (new static())->with($with)->where($filter)->find();
    }

    /**
     * 详情
     */
    public static function detailWithOrder($where)
    {
        $list = (new static())->where($where)->select();
        $orderList = [];
        foreach ($list as $trade){
            array_push($orderList, Order::detail($trade['order_id']));
        }
        return $orderList;
    }
}
